<template>
  <div class="registPage">
    <van-nav-bar
      title="欢迎注册实训云"
      left-text="返回登录"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="teacherName"
        name="teacherName"
        label="教师姓名"
        placeholder="请输入姓名"
        :rules="[{ required: true}]"
      />
      <van-field name="gender" label="单选框">
        <template #input>
          <van-radio-group v-model="gender" direction="horizontal">
            <van-radio name="1">男</van-radio>
            <van-radio name="2">女</van-radio>
          </van-radio-group>
        </template>
      </van-field>
      <van-field
        v-model="teacherNum"
        name="teacherNum"
        label="教师工号"
        placeholder="请输入教师工号"
        :rules="[{ required: true}]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>



  </div>
</template>

<script>
import { regiser } from '../api/user.js'
import { Toast } from 'vant'

export default {
  name: 'Regist',
  data: function() {
    return {
      msg: '主页',
      teacherName: '',
      teacherNum: '',
      password: '',
      gender: '2',
      errMsg: ''
    }
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1)
    },
    onSubmit(values) {
      console.log('submit', values)
      // 包装参数
      const params = {
        teacherName: values.teacherName,
        teacherNum: values.teacherNum,
        password: values.password,
        gender: values.gender
      }
      console.log('包装参数', params)
      // 异步 register(params).then((参数)=>{执行的函数})
      regiser(params).then((res) => {
        Toast(res.msg)
        this.$router.push('/login')
      })
    }
  }
}

</script>

  <style scoped>
    .registInput {
      margin: 20px;
      margin-top: 80px
    }
  </style>

